import { encrypt, decrypt } from "@/utils/aes";

/**
 * @name proxy
 * @desc 域名地址，代理前缀
 */
function proxys() {
  return {
    devProxy: "http://edu1.zhzy.net.cn",
    testProxy: "http://test.guoli-edu.com",
    provProxy: "http://edu.guoli-edu.com"
  };
}

/**
 * @name objectToSearch
 * @param {Object} value 地址栏传递内容
 * @desc 将value转化为加密字符串拼接到url后缀
 */
function objectToSearch(value) {
  if (value) {
    let encrypted = "?" + encrypt(value);
    return encrypted;
  }
}

/**
 * @name getSearchObject
 * @param {String} window.location.search
 * @desc 获取url中search参数,并转化为对象
 */
function getSearchObject() {
  let value = window.location.search.split("?");
  if (value.length > 1) {
    let str = value[1];
    let decrypted = decrypt(str);
    return decrypted;
  } else {
    return {};
  }
}
/**
 * @name randomNum
 * @param {Number} num 生成几位随机数
 * @param Math.random() 0-1之间的随机数
 * @param Math.floor() 向下取整
 * @description 随机数生成
 */

function randomNum(num) {
  let index = num && Number(num) ? Number(num) : 18; //默认生成18位随机数
  let randoms = "";
  for (let i = 0; i < index; i++) {
    randoms += JSON.stringify(Math.floor(Math.random() * 10));
  }
  return randoms;
}
/**
 * @name operation
 * @param {Boolean} type true/false
 * @description 运算，取小数点后两位
 */
function operation(val, type) {
  let num = 0;
  if (type) {
    num = Math.round(val);
  } else {
    num = Math.round(val * 100) / 100;
  }
  return num;
}
/**
 * @name unitConver
 * @param {Number} val 原始大小 单位 B,KB,M,G,T
 * 1024;1048576;1073741824;1099511627776;1125899906842624
 */
function unitConver(val) {
  if (val && Number(val)) {
    let num = Number(val);
    let res = 0;
    if (num < 1024) {
      res = `${num}B`;
    } else if (num >= 1024 && num < 1048576) {
      res = `${operation(num / 1024, true)}KB`;
    } else if (num >= 1048576 && num < 1073741824) {
      res = `${operation(num / 1024 / 1024)}M`;
    } else if (num >= 1073741824 && num < 1125899906842624) {
      res = `${operation(num / 1024 / 1024 / 1024)}G`;
    } else if (num >= 1125899906842624) {
      res = `${operation(num / 1024 / 1024 / 1024 / 1024)}T`;
    }
    return res;
  }
}

export { proxys, objectToSearch, getSearchObject, randomNum, unitConver };
